<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- saved from url=(0070)http://www.exampledepot.com/egs/javax.swing.filechooser/CreateDlg.html -->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" class="js chrome  win"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

  <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">  
  
<meta name="description" content="Java Code Examples from The Java Developers Almanac">
<meta name="keywords" content="Java,examples">
<meta name="owner" content="Patrick Chan">
  <title>Creating a JFileChooser Dialog | Example Depot</title>  
  <link type="text/css" rel="stylesheet" media="all" href="./Creating a JFileChooser Dialog   Example Depot_files/views.css">
<link type="text/css" rel="stylesheet" media="all" href="./Creating a JFileChooser Dialog   Example Depot_files/css_36814bcb1cc37de50adba89202fe8f15.css">
  <script type="text/javascript" src="./Creating a JFileChooser Dialog   Example Depot_files/js_546e7acd2b7824607f511e9fa6808463.js"></script>
<script type="text/javascript">
<!--//--><![CDATA[//><!--
jQuery.extend(Drupal.settings, { "basePath": "/" });
//--><!]]>
</script>
  <!--[if IE 6]><link rel="stylesheet" href="/sites/all/themes/artisteer_exd/style.ie6.css" type="text/css" /><![endif]-->  
  <!--[if IE 7]><link rel="stylesheet" href="/sites/all/themes/artisteer_exd/style.ie7.css" type="text/css" media="screen" /><![endif]-->
  <script type="text/javascript">if (Drupal.jsEnabled) {$(document).ready(function(){
				window.setTimeout("artNoStyleAdding(document)", 2000);});}</script>
  <script type="text/javascript"> </script>
<script src="chrome-extension://lifbcibllhkdhoafpjfnlhfpfgnpldfl/document_iterator.js"></script><script src="chrome-extension://lifbcibllhkdhoafpjfnlhfpfgnpldfl/find_proxy.js"></script><script src="chrome-extension://lifbcibllhkdhoafpjfnlhfpfgnpldfl/get_html_text.js"></script><script src="chrome-extension://lifbcibllhkdhoafpjfnlhfpfgnpldfl/global_constants.js"></script><script src="chrome-extension://lifbcibllhkdhoafpjfnlhfpfgnpldfl/name_injection_builder.js"></script><script src="chrome-extension://lifbcibllhkdhoafpjfnlhfpfgnpldfl/number_injection_builder.js"></script><script src="chrome-extension://lifbcibllhkdhoafpjfnlhfpfgnpldfl/string_finder.js"></script><script src="chrome-extension://lifbcibllhkdhoafpjfnlhfpfgnpldfl/change_sink.js"></script><meta name="document_iterator.js"><meta name="find_proxy.js"><meta name="get_html_text.js"><meta name="global_constants.js"><meta name="name_injection_builder.js"><meta name="number_injection_builder.js"><meta name="string_finder.js"><meta name="change_sink.js"></head>

<body>
<div id="art-main">
<div class="art-Sheet">
    <div class="art-Sheet-tl"></div>
    <div class="art-Sheet-tr"></div>
    <div class="art-Sheet-bl"></div>
    <div class="art-Sheet-br"></div>
    <div class="art-Sheet-tc"></div>
    <div class="art-Sheet-bc"></div>
    <div class="art-Sheet-cl"></div>
    <div class="art-Sheet-cr"></div>
    <div class="art-Sheet-cc"></div>
    <div class="art-Sheet-body">
<div class="art-Header">
    <div class="art-Header-jpeg"></div>
<div class="art-Logo">
    <h1 class="art-Logo-name"><a href="http://www.exampledepot.com/" title="Example Depot">Example Depot</a></h1><div class="art-Logo-text">Useful code samples</div></div>
</div>
<div class="art-nav">
        <div class="l"></div>
    <div class="r"></div>
            <ul class="art-menu">
<li class="leaf first"><a href="http://www.exampledepot.com/" title=""><span class="l"> </span><span class="r"> </span><span class="t">Home</span></a></li><li><span class="art-menu-separator"></span></li>
<li class="leaf"><a href="http://www.exampledepot.com/egs" title=""><span class="l"> </span><span class="r"> </span><span class="t">Java</span></a></li><li><span class="art-menu-separator"></span></li>
<li class="leaf"><a href="http://www.exampledepot.com/contact" title=""><span class="l"> </span><span class="r"> </span><span class="t">Contact</span></a></li><li><span class="art-menu-separator"></span></li>
<li class="leaf last"><a href="http://www.exampledepot.com/blog" title=""><span class="l"> </span><span class="r"> </span><span class="t">Blog</span></a></li>
</ul>
	</div>
<div class="art-contentLayout">
<div class="art-sidebar1"><div class="art-Block clear-block block block-search" id="block-search-0">
    <div class="art-Block-cc"></div>
    <div class="art-Block-body">

	<div class="art-BlockContent content">
	    <div class="art-BlockContent-body">
	
		<form action="./Creating a JFileChooser Dialog   Example Depot_files/Creating a JFileChooser Dialog   Example Depot.htm" accept-charset="UTF-8" method="post" id="search-block-form">
<div><div class="form-item" id="edit-search-block-form-1-wrapper">
 <input type="text" maxlength="128" name="search_block_form" id="edit-search-block-form-1" size="15" value="" title="Enter the terms you wish to search for." class="form-text">
</div>
<span class="art-button-wrapper"><span class="l"></span><span class="r"></span><input type="submit" name="op" id="edit-submit" value="Search" class="form-submit art-button"></span><input type="hidden" name="form_build_id" id="form-969532b22dcbf662487d8df80a97df92" value="form-969532b22dcbf662487d8df80a97df92">
<input type="hidden" name="form_id" id="edit-search-block-form" value="search_block_form">

</div></form>

	    </div>
	</div>
	

    </div>
</div>
<div class="art-Block clear-block block block-adsense_managed" id="block-adsense_managed-0">
    <div class="art-Block-cc"></div>
    <div class="art-Block-body">

	<div class="art-BlockContent content">
	    <div class="art-BlockContent-body">
	
		<div style="text-align:center"><div class="adsense adsense_managed">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-6001183370374757";
/* 160x600 */
google_ad_slot = "6138857183";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript" src="./Creating a JFileChooser Dialog   Example Depot_files/show_ads.js">
</script><ins style="display:inline-table;border:none;height:600px;margin:0;padding:0;position:relative;visibility:visible;width:160px"><ins id="aswift_0_anchor" style="display:block;border:none;height:600px;margin:0;padding:0;position:relative;visibility:visible;width:160px"><iframe allowtransparency="true" frameborder="0" height="600" hspace="0" marginwidth="0" marginheight="0" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&amp;&amp;s.handlers,h=H&amp;&amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;&amp;d&amp;&amp;(!d.body||!d.body.firstChild)){if(h.call){i+=&#39;.call&#39;;setTimeout(h,0)}else if(h.match){i+=&#39;.nav&#39;;w.location.replace(h)}s.log&amp;&amp;s.log.push(i)}" scrolling="no" vspace="0" width="160" id="aswift_0" name="aswift_0" style="left:0;position:absolute;top:0;"></iframe></ins></ins>
</div></div>
	    </div>
	</div>
	

    </div>
</div>
</div><div class="art-content">
              
<div class="art-Post">
    <div class="art-Post-body">
<div class="art-Post-inner">
<div class="art-PostContent">
<div class="breadcrumb"><a href="http://www.exampledepot.com/egs">Java</a> » <a href="http://www.exampledepot.com/egs/index.html">Java packages</a> » <a href="http://www.exampledepot.com/egs/javax.swing.filechooser/pkg.html">javax.swing.filechooser</a></div>
</div>
<div class="cleared"></div>

</div>

    </div>
</div>
<div class="art-Post">
    <div class="art-Post-body">
<div class="art-Post-inner">
<h2 class="art-PostHeader"> <a href="./Creating a JFileChooser Dialog   Example Depot_files/Creating a JFileChooser Dialog   Example Depot.htm" title="Creating a JFileChooser Dialog">Creating a JFileChooser Dialog</a>
</h2>
<div class="art-PostContent">
<div class="art-article"><!-- google_ad_section_start -->
<div class="exd_ex">
The following example creates a file chooser and displays it as first
an open-file dialog and then as a save-file dialog:



<div class="exd_code">
<div class="exd_code_head"><a href="">COPY</a></div>
<pre class="exd_code_body">String filename = <span class="exd_v">File.separator+"tmp"</span>;
JFileChooser fc = new JFileChooser(new File(filename));

// Show open dialog; this method does not return until the dialog is closed
fc.showOpenDialog(<span class="exd_v">frame</span>);
File selFile = fc.getSelectedFile();

// Show save dialog; this method does not return until the dialog is closed
fc.showSaveDialog(<span class="exd_v">frame</span>);
selFile = fc.getSelectedFile();
</pre></div>

Here is a more elaborate example that creates two buttons that 
create and show file chooser dialogs.



<div class="exd_code">
<div class="exd_code_head"><a href="">COPY</a></div>
<pre class="exd_code_body">// This action creates and shows a modal open-file dialog.
public class OpenFileAction extends AbstractAction {
    JFrame frame;
    JFileChooser chooser;

    OpenFileAction(JFrame frame, JFileChooser chooser) {
        super("Open...");
        this.chooser = chooser;
        this.frame = frame;
    }

    public void actionPerformed(ActionEvent evt) {
        // Show dialog; this method does not return until dialog is closed
        chooser.showOpenDialog(frame);

        // Get the selected file
        File file = chooser.getSelectedFile();
    }
};

// This action creates and shows a modal save-file dialog.
public class SaveFileAction extends AbstractAction {
    JFileChooser chooser;
    JFrame frame;

    SaveFileAction(JFrame frame, JFileChooser chooser) {
        super("Save As...");
        this.chooser = chooser;
        this.frame = frame;
    }

    public void actionPerformed(ActionEvent evt) {
        // Show dialog; this method does not return until dialog is closed
        chooser.showSaveDialog(frame);

        // Get the selected file
        File file = chooser.getSelectedFile();
    }
};
</pre></div>
Here's some code that demonstrates the use of the actions:


<div class="exd_code">
<div class="exd_code_head"><a href="">COPY</a></div>
<pre class="exd_code_body">JFrame frame = new JFrame();

// Create a file chooser
String filename = <span class="exd_v">File.separator+"tmp"</span>;
JFileChooser fc = new JFileChooser(new File(filename));

// Create the actions
Action openAction = new OpenFileAction(<span class="exd_v">frame</span>, fc);
Action saveAction = new SaveFileAction(<span class="exd_v">frame</span>, fc);

// Create buttons for the actions
JButton openButton = new JButton(openAction);
JButton saveButton = new JButton(saveAction);

// Add the buttons to the frame and show the frame
frame.getContentPane().add(openButton, BorderLayout.NORTH);
frame.getContentPane().add(saveButton, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
</pre></div>
</div>
<!-- google_ad_section_end --></div>
</div>
<div class="cleared"></div>

</div>

    </div>
</div>
<div class="art-Block clear-block block block-views" id="block-views-related_examples-block_1">
    <div class="art-Block-cc"></div>
    <div class="art-Block-body">

	<div class="art-BlockHeader">
		    <div class="l"></div>
		    <div class="r"></div>
		    <div class="art-header-tag-icon">
		        <div class="t">	
			<h2 class="subject">Related examples</h2>
</div>
		    </div>
		</div>    
	<div class="art-BlockContent content">
	    <div class="art-BlockContent-body">
	
		<div class="view view-related-examples view-id-related_examples view-display-id-block_1 view-dom-id-1">
    
  
  
      <div class="view-content">
        <div class="views-row views-row-1 views-row-odd views-row-first">
      
  <div class="views-field-title">
                <span class="field-content"><a href="http://www.exampledepot.com/egs/javax.swing.filechooser/Filter.html">Adding a Filter to a File Chooser Dialog</a></span>
  </div>
  </div>
  <div class="views-row views-row-2 views-row-even">
      
  <div class="views-field-title">
                <span class="field-content"><a href="http://www.exampledepot.com/egs/javax.swing.filechooser/Copy.html">Copying the Filename Path from a JFileChooser Dialog to the Clipboard</a></span>
  </div>
  </div>
  <div class="views-row views-row-3 views-row-odd">
      
  <div class="views-field-title">
                <span class="field-content"><a href="http://www.exampledepot.com/egs/javax.swing.filechooser/Done.html">Determining If the Approve or Cancel Button Was Clicked in a JFileChooser Dialog</a></span>
  </div>
  </div>
  <div class="views-row views-row-4 views-row-even">
      
  <div class="views-field-title">
                <span class="field-content"><a href="http://www.exampledepot.com/egs/javax.swing.filechooser/DirOnly.html">Displaying Only Directories in a File Chooser Dialog</a></span>
  </div>
  </div>
  <div class="views-row views-row-5 views-row-odd">
      
  <div class="views-field-title">
                <span class="field-content"><a href="http://www.exampledepot.com/egs/javax.swing.filechooser/CurDir.html">Getting and Setting the Current Directory of a JFileChooser Dialog</a></span>
  </div>
  </div>
  <div class="views-row views-row-6 views-row-even">
      
  <div class="views-field-title">
                <span class="field-content"><a href="http://www.exampledepot.com/egs/javax.swing.filechooser/CurFile.html">Getting and Setting the Selected File of a JFileChooser Dialog</a></span>
  </div>
  </div>
  <div class="views-row views-row-7 views-row-odd views-row-last">
      
  <div class="views-field-title">
                <span class="field-content"><a href="http://www.exampledepot.com/egs/javax.swing.filechooser/GetFileType.html">Getting the File-Type Name of a File</a></span>
  </div>
  </div>
    </div>
  
  
  
  
  
  
</div> 
	    </div>
	</div>
	

    </div>
</div>
<div class="art-Block clear-block block block-views" id="block-views-comments-block_1">
    <div class="art-Block-cc"></div>
    <div class="art-Block-body">

	<div class="art-BlockHeader">
		    <div class="l"></div>
		    <div class="r"></div>
		    <div class="art-header-tag-icon">
		        <div class="t">	
			<h2 class="subject">Comments</h2>
</div>
		    </div>
		</div>    
	<div class="art-BlockContent content">
	    <div class="art-BlockContent-body">
	
		<div class="view view-comments view-id-comments view-display-id-block_1 view-dom-id-2">
    
  
  
      <div class="view-content">
        <div class="views-row views-row-1 views-row-odd views-row-first">
    <a id="comment-15285"></a>
<div class="exd-comment">
  <table class="exd-comment-header" width="100%">
    <tbody>
	<tr>
	  <td>
	    10 Sep 2010 - 3:14am by Anonymous (not verified)	  </td>
	  <td align="right">
	    	  </td>
	</tr>
	</tbody>
  </table>
  <div class="exd-comment-content">
	<p>i want a source code for compress the image</p>
  </div>
</div>
  </div>
  <div class="views-row views-row-2 views-row-even views-row-last">
    <a id="comment-42157"></a>
<div class="exd-comment">
  <table class="exd-comment-header" width="100%">
    <tbody>
	<tr>
	  <td>
	    14 Jan 2011 - 2:17am by good boy (not verified)	  </td>
	  <td align="right">
	    	  </td>
	</tr>
	</tbody>
  </table>
  <div class="exd-comment-content">
	<p>good</p>
  </div>
</div>
  </div>
    </div>
  
  
  
  
  
  
</div> 
	    </div>
	</div>
	

    </div>
</div>
<div class="art-Block clear-block block block-commentblock" id="block-commentblock-comment_form">
    <div class="art-Block-cc"></div>
    <div class="art-Block-body">

	<div class="art-BlockHeader">
		    <div class="l"></div>
		    <div class="r"></div>
		    <div class="art-header-tag-icon">
		        <div class="t">	
			<h2 class="subject">Post a comment</h2>
</div>
		    </div>
		</div>    
	<div class="art-BlockContent content">
	    <div class="art-BlockContent-body">
	
		<form action="http://www.exampledepot.com/comment/reply/2085" accept-charset="UTF-8" method="post" id="comment-form">
<div><div class="form-item" id="edit-name-wrapper">
 <label for="edit-name">Your name: </label>
 <input type="text" maxlength="60" name="name" id="edit-name" size="30" value="Anonymous" class="form-text">
</div>
<div class="form-item" id="edit-comment-wrapper">
 <label for="edit-comment">Comment: <span class="form-required" title="This field is required.">*</span></label>
 <div class="resizable-textarea"><span><textarea cols="60" rows="3" name="comment" id="edit-comment" class="form-textarea resizable required textarea-processed"></textarea><div class="grippie" style="margin-right: -6px; "></div></span></div>
</div>
<p><a href="http://www.exampledepot.com/filter/tips">More information about formatting options</a></p><input type="hidden" name="form_build_id" id="form-6a0e9732010da9ae26ee73bcc6e85989" value="form-6a0e9732010da9ae26ee73bcc6e85989">
<input type="hidden" name="form_id" id="edit-comment-form" value="comment_form">
<fieldset class="captcha"><legend>CAPTCHA</legend><div class="description">This question is for testing whether you are a human visitor and to prevent automated spam submissions.</div><input type="hidden" name="captcha_sid" id="edit-captcha-sid" value="13867946">
<img src="./Creating a JFileChooser Dialog   Example Depot_files/1311316028" alt="Image CAPTCHA" title="Image CAPTCHA"><div class="form-item" id="edit-captcha-response-wrapper">
 <label for="edit-captcha-response">What code is in the image?: <span class="form-required" title="This field is required.">*</span></label>
 <input type="text" maxlength="128" name="captcha_response" id="edit-captcha-response" size="15" value="" class="form-text required">
 <div class="description">Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.</div>
</div>
</fieldset>
<span class="art-button-wrapper"><span class="l"></span><span class="r"></span><input type="submit" name="op" id="edit-submit-1" value="Post" class="form-submit art-button"></span>
</div></form>

	    </div>
	</div>
	

    </div>
</div>
</div>

</div>
<div class="cleared"></div>
<div class="art-Footer">
    <div class="art-Footer-inner">
        <a href="http://www.exampledepot.com/rss.xml" class="art-rss-tag-icon"></a>        <div class="art-Footer-text">
        <p><a href="http://www.exampledepot.com/user">Login</a>&nbsp;|&nbsp;<a href="http://www.exampledepot.com/egs/javax.swing.filechooser/CreateDlg.html#">Terms of Use</a>&nbsp;|&nbsp;<a href="http://www.exampledepot.com/egs/javax.swing.filechooser/CreateDlg.html#">Trademarks</a>&nbsp;|&nbsp;<a href="http://www.exampledepot.com/egs/javax.swing.filechooser/CreateDlg.html#">Privacy Statement</a><br>Copyright © 2009&nbsp;Example Depot.&nbsp;All Rights Reserved.</p>                </div>        
    </div>
    <div class="art-Footer-background"></div>
</div>

    </div>
</div>
<div class="cleared"></div>

</div>




<div style="position: absolute; left: 414px; top: 254px; width: 22px; height: 10px; z-index: 99; "><embed id="ZeroClipboardMovie_1" src="/sites/all/modules/exampledepot/zeroclipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="22" height="10" name="ZeroClipboardMovie_1" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&amp;width=22&amp;height=10" wmode="transparent"></div><div style="position: absolute; left: 414px; top: 496px; width: 22px; height: 10px; z-index: 99; "><embed id="ZeroClipboardMovie_2" src="/sites/all/modules/exampledepot/zeroclipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="22" height="10" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&amp;width=22&amp;height=10" wmode="transparent"></div><div style="position: absolute; left: 414px; top: 1231px; width: 22px; height: 10px; z-index: 99; "><embed id="ZeroClipboardMovie_3" src="/sites/all/modules/exampledepot/zeroclipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="22" height="10" name="ZeroClipboardMovie_3" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&amp;width=22&amp;height=10" wmode="transparent"></div></body><span id="skype_highlighting_settings" display="none" autoextractnumbers="1"></span><object id="skype_plugin_object" location.href="http://www.exampledepot.com/egs/javax.swing.filechooser/CreateDlg.html" location.hostname="www.exampledepot.com" style="position: absolute; visibility: hidden; left: -100px; top: -100px; " width="0" height="0" type="application/x-vnd.skype.toolbars.npplugin.4.2.0"></object></html>